<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
use app\common\model\Document;
use app\common\model\Wpdocument;
use app\common\model\SearchLog;
class Wpgroup extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {   
        if($this->request->isAjax()){
            $page = $this->request->post('page',1);
            $keywords = $this->request->post('keywords','');
            $type = $this->request->post('type','');
            $nav = $this->request->post('nav','');
            $where['d.status'] = 'normal';
            $where['d.is_audit'] = 1;
            if($keywords){
                $searchlog = SearchLog::get(['name'=>$keywords]);
                if(empty($searchlog)){
                    $searchlog = new Searchlog();
                    $searchlog->name = $keywords;
                    $searchlog->nums = 1;
                    
                }else{
                    $searchlog->nums = $searchlog['nums'] + 1;
                }
                $searchlog->save();
                $where['d.title|d.content'] = ['like','%'.$keywords.'%'];
            }
            if($type){
                $where['d.type_id'] = $type;
            }
            if($nav){
                $where['d.cate_id'] = $nav;
            }
            $list = Db::name('Wpdocument')->where($where)->alias('d')->join('wd_wptype t','t.id = d.type_id')->join('wd_wpcate c','d.cate_id =c.id')
            ->order('d.createtime desc')->page($page,10)->field('d.title,d.createtime,d.content,c.name as cate_name,t.name as type_name,d.id,d.image')->select();
            foreach ($list as $key => &$value) {
                $value['createtime'] = date('Y-m-d H:i',$value['createtime']);
            }
            return json(['code'=>200,'data'=>$list,'msg'=>'success']);
        }
        $user = Db::name('User')->alias('u')->join('wd_wpdocument d','d.user_id =u.id','LEFT')->where('u.status','normal')->where('u.group_id',2)
        ->field('u.id,u.nickname,u.avatar,count(d.id) as nums')->group('u.id')->order('nums desc')->select();
        $total['user'] = count($user);
        $total['article'] = Db::name('Wpdocument')->where('status','normal')->count('id');
        $this->assign('total',$total);
        $this->assign('duser',$user);
        return $this->view->fetch();
    }
    
    public function detail(){
        if($this->request->isAjax()){
            $id = $this->request->post('id',0);
            $where['id'] = $id;
            $data =Db::name('Wpdocument')->where($where)->find();
            $data['createtime'] = date('Y-m-d H:i:s',$data['createtime']);
            $user = Db::name('User')->where('id',$data['user_id'])->field('nickname,id,avatar,group_id')->find();
            $user['group_image'] = $user['group_id']?Db::name('UserGroup')->where('id','in',$user['group_id'])->select():[];
            $user['doc'] =Db::name('Document')->where('user_id',$user['id'])->where('is_audit',1)->count('id');
            $user['featured'] =Db::name('Document')->where('user_id',$user['id'])->where('is_featured',1)->count('id');
            $user['attention'] =Db::name('UserAttention')->where('user_id',$user['id'])->count('id');
            $data['user'] = $user;
            $data['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
            ->where('d.type','wd')->where('d.doc_id',$data['id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
            $data['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
            ->where('d.type','wd')->where('d.doc_id',$data['id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->select();
            if($this->auth->id && $this->auth->level > 0){

            }else{
                $data['content'] = mb_substr(strip_tags($data['content']),0,200).'...';
            }
            return json(['code'=>200,'data'=>$data,'msg'=>'success']);
        
        }
        $id = $this->request->param('id');
        Db::name('Wpdocument')->where('id',$id)->setInc('clicks',1);
        Db::name('Document')->where('doc_id',$id)->where('type','wp')->setInc('clicks',1);
        Db::name('Wpdocument')->where('id',$id)->setInc('once_clicks',1);
        Db::name('Document')->where('doc_id',$id)->where('type','wp')->setInc('once_clicks',1);
        $this->assign('id',$id);
        return $this->view->fetch();
    }

    public function add(){
        if($this->request->isAjax()){
            
            $params = $this->request->post('row/a');
            Db::startTrans();
            try {
                $params['user_id'] = (int)$this->auth->id;
                $params['status'] = 'normal';
                $doc = Wpdocument::create($params,true);

                $document = $doc->toArray();
                $document['doc_id'] = $document['id'];
                unset($document['id']);
                unset($document['type_id']);
                unset($document['cate_id']);
                $document['type'] = 'wp';
                Document::create($document,true);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success();   
        }
        $cate = Db::name('Wpcate')->where('status','normal')->column('name','id');
        $type = Db::name('Wptype')->where('status','normal')->column('name','id');
        $this->assign('cate',$cate);
        $this->assign('type',$type);
        return $this->view->fetch();
    }

}
